<!-- 随附单据 弹窗 -->
<template>
    <Modal v-model="accompanyDoc" width="746" :closable="false" footer-hide>
        <div slot="header">
            随附单据
            <Icon class="f-fr" type="ios-close" @click="closeModel"/>
        </div>
        <Form class="clearfix form-col-2" :label-width="124" inline>
            <FormItem label="附件类型">
                <Select v-model="accompanyType" transfer filterable>
                    <Option
                        v-for="item in accessoryTypeList"
                        :key="item.value"
                        :value="item.code"
                        :label="item.name"
                    >
                    </Option>
                </Select>
            </FormItem>
            <FormItem label="上传附件">
                <Upload
                    v-if="!loadFlag"
                    :headers="header"
                    :action="fileUploadUrl"
                    :show-upload-list="false"
                    :on-success="uploadSuccess"
                    :before-upload="beforeUpload"
                    accept=".xls,.xlsx,.pdf"
                    :format="['xls','xlsx','pdf']"
                    :max-size="4096"
                    :on-error="uploadError"
                >
                    <Button @click="uploadHandle" type="primary">上传附件</Button>
                </Upload>
                <Button :loading="loadFlag" v-if="loadFlag" type="primary">上传附件</Button>
            </FormItem>
        </Form>
        <Button class="icon-btn" type="primary" @click="excelToPdf">转换PDF</Button>
        <Button style="margin-left: 14px;" type="primary" @click="removeHandle">删除</Button>
        <Button style="margin-left: 14px;" type="primary" @click="downLoadHandle">下载</Button>
        <div class="accompany-table-Modal">
            <Table
                @on-selection-change="accompanyChange"
                :columns="accompanyColumn"
                :data="accompanyDataList"
            ></Table>
        </div>
    </Modal>
</template>

<script>
import { getToken } from '@/libs/util.js'
import { queryFile, delDataFile, excelTopdf } from '@/api/declare'
import formatTime from '@/libs/formatTime'
import { downloadURL } from '@/config/downloadUrl.js'

export default {
    name: 'AccompanyDoc',
    data () {
        return {
            formatTime,
            accompanyDoc: false,
            accompanyType: '00000001',
            accessoryTypeList: [
                {
                    'name': '发票',
                    'code': '00000001'
                },
                {
                    'name': '装箱单',
                    'code': '00000002'
                },
                {
                    'name': '提运单',
                    'code': '00000003'
                },
                {
                    'name': '合同',
                    'code': '00000004'
                },
                {
                    'name': '纸质委托书',
                    'code': '00000008'
                },
                {
                    'name': '原产地证据文件',
                    'code': '00000009'
                },
                {
                    'name': '载货清单(舱单)',
                    'code': '00000010'
                },
                {
                    'name': '电子委托书',
                    'code': '10000001'
                },
                {
                    'name': '减免税货物税款担保证明',
                    'code': '10000002'
                },
                {
                    'name': '减免税货物税款担保延期证明',
                    'code': '10000003'
                }
            ],
            accompanyColumn: [
                {
                    type: 'selection',
                    align: 'center'
                },
                {
                    title: '文件名称',
                    key: 'fileName',
                    tooltip: true
                },
                {
                    title: '类别',
                    key: 'foleName'
                },
                {
                    title: '上传人',
                    key: 'creator'
                },
                {
                    title: '上传时间',
                    key: 'createTimeDec',
                    tooltip: true
                }
            ],
            accompanyDataList: [],
            selectedDataList: [],
            loadFlag: false,
            header: {
                Authorization: ''
            },
            fileUploadUrl: '',
            declareId: ''
        }
    },
    methods: {
        init (declareId) {
            this.accompanyDoc = true
            this.declareId = declareId
            this.getAccompanyData()
        },

        getAccompanyData () {
            let params = {
                declareId: this.declareId
            }
            queryFile(params).then(res => {
                if (res.data.code === '0000') {
                    this.selectedDataList = []
                    this.accompanyDataList = res.data.data
                    for (let i = 0; i < this.accompanyDataList.length; i++) {
                        for (let j = 0; j < this.accessoryTypeList.length; j++) {
                            if (this.accompanyDataList[i].fileChildType === this.accessoryTypeList[j].code) {
                                this.accompanyDataList[i].foleName = this.accessoryTypeList[j].name
                            }
                        }
                    }
                }
            })
        },

        closeModel () {
            this.accompanyDoc = false
        },

        accompanyChange (val) {
            this.selectedDataList = val
        },

        assembleData () {
            let nameArr = []
            let typeArr = []
            this.selectedDataList.forEach(item => {
                nameArr.push(item.fileName)
                typeArr.push(item.fileChildType)
            })
            let params = {
                declareId: this.declareId,
                fileNames: nameArr.join(),
                fileChildTypes: typeArr.join()
            }
            return params
        },

        excelToPdf () {
            if (this.selectedDataList.length === 0) {
                this.$Message.warning('请勾选文件名称')
                return false
            }
            let params = this.assembleData()
            excelTopdf(params).then(res => {
                if (res.data.code === '0000') {
                    this.$Message.success(res.data.msg)
                } else {
                    this.$Message.warning(res.data.msg)
                }
            })
        },

        removeHandle () {
            if (this.selectedDataList.length === 0) {
                this.$Message.warning('请勾选文件名称')
                return false
            }
            let params = this.assembleData()
            delDataFile(params).then(res => {
                if (res.data.code === '0000') {
                    this.$Message.success(res.data.msg)
                    this.getAccompanyData()
                } else {
                    this.$Message.warning(res.data.msg)
                }
            })
        },

        downLoadHandle () {
            if (this.selectedDataList.length === 0) {
                this.$Message.warning('请勾选文件名称')
                return false
            }
            let params = this.assembleData()
            let token = getToken()
            window.open(downloadURL + '/ui/dataFileDownload/downloadFile?Authorization=' + token + '&params=' + encodeURI(JSON.stringify(params)))
        },

        uploadSuccess (res) {
            this.loadFlag = false
            if (res.code === '0000') {
                this.$Message.success(res.msg)
                this.getAccompanyData()
            } else {
                this.$Message.warning(res.msg)
            }
        },

        beforeUpload (file) {
            this.loadFlag = true
            if (!/\.(xls|xlsx|pdf)$/.test(file.name)) {
                this.$Message.warning('文件类型不正确')
                this.loadFlag = false
            }
        },

        uploadError () {
            this.$Message.error('文件上传失败,请重新上传')
            this.loadFlag = false
        },

        uploadHandle () {
            this.header.Authorization = getToken()
            this.fileUploadUrl = '/ui/customs/declare/uploadFile?relatedId=' + this.declareId + '&fileChildType=' + this.accompanyType
        }
    }
}
</script>

<style lang="less">
    .accompany-table-Modal {
        margin: 15px 0;
    }
</style>
